﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class SuppleMent_Controls_COMMODITY_LINK : System.Web.UI.UserControl
{
    private string spdm;
    public string SPDM
    {
        get
        {
            if (ViewState["SPDM"] != null)
            {
                spdm = ViewState["SPDM"].ToString();
            }
            return spdm;
        }
        set
        {
            ViewState["SPDM"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        BindAspNetPage1(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SPDM);
    }
    protected void btnSearch2_Click(object sender, EventArgs e)
    {
        BindAspNetPage2(AspNetPager2.PageSize, GetStartIndex(AspNetPager2.CurrentPageIndex, AspNetPager2.PageSize), SPDM);
    }
    //新增关联产品
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300100", "00017"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        try
        {
            List<string> strproductid = ShangpinList1.Selected;
            string strproductidlist = string.Empty;
            if (strproductid.Count != 0)
            {
                foreach (string productid in strproductid)
                {
                    if (SPDM != productid)
                    {
                        strproductidlist += productid + ",";
                    }
                }
                new BLL.COMMODITY_LINK().Add(SPDM, strproductidlist);
                Response.Redirect(@"~/BaseData/ProductList.aspx?key=" + SPDM + "&commLink=tt&type=0");
            }
            else
            {
                Share.Alert(this.Page,"请选择关联商品");
                return;
            }
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page,"新增关联商品失败");
            return;
        }
    }
    //删除关联产品
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300100", "00003"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        try 
        {
            List<string> productid = COMMODITY_LINKList1.Selected;
            if (productid.Count != 0)
            {
                List<int> ID = new List<int>();
                foreach (string proid in productid)
                {
                    //存在关联商品则删除该关联
                    Model.COMMODITY_LINK link = new BLL.COMMODITY_LINK().GetLink(SPDM,proid);
                    if (link != null)
                    {
                        ID.Add(link.ID);
                    }
                }
                new BLL.COMMODITY_LINK().Delete(ID);
                Response.Redirect(@"~/BaseData/ProductList.aspx?key=" + SPDM + "&commLink=tt&type=0");
            }
            else
            {
                Share.Alert(this.Page, "请选择关联商品");
                return;
            }
        }
        catch
        {
            Share.Alert(this.Page, "取消关联商品失败");
            return;
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindAspNetPage1(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SPDM);
    }
    protected void AspNetPager2_PageChanged(object sender, EventArgs e)
    {
        BindAspNetPage2(AspNetPager2.PageSize, GetStartIndex(AspNetPager2.CurrentPageIndex, AspNetPager2.PageSize), SPDM);
    }
    public void Bind(string spdm)
    {
        BindAspNetPage1(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SPDM);
        BindAspNetPage2(AspNetPager2.PageSize, GetStartIndex(AspNetPager2.CurrentPageIndex, AspNetPager2.PageSize), SPDM);

    }
    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }
    private void BindAspNetPage2(int pagesize, int pageindex, string spdm)
    {
        //绑定商品关联列表
        int rowCount;
        string strsql = string.Empty;
        if (txtSearch.Text != string.Empty)
        {
            if (DropDownList1.SelectedValue == "0")
            {
                strsql += " and SPDM like '%" + txtSearch.Text.Trim() + "%' ";
            }
            else if (DropDownList1.SelectedValue == "1")
            {
                strsql += " and SPMC like '%" + txtSearch.Text.Trim() + "%' ";
            }
        }
        DataSet comLink = new BLL.COMMODITY_LINK().GetList(pagesize, pageindex, strsql, spdm, out rowCount);
        AspNetPager2.RecordCount = rowCount;
        COMMODITY_LINKList1.Bind(comLink.Tables[0]);
    }
    private void BindAspNetPage1(int pagesize, int pageindex, string spdm)
    {
        int pageCount, rowCount;
        string StrSql = "";
        if (TextBox1.Text.Trim() != "")
        {
            if (DropDownList1.SelectedValue == "0")
            {
                StrSql += " and SPDM like '%" + TextBox1.Text.Trim() + "%' ";
            }
            else if (DropDownList1.SelectedValue == "1")
            {
                StrSql += " and SPMC like '%" + TextBox1.Text.Trim() + "%' ";
            }
            else if (DropDownList1.SelectedValue == "2")
            {
                StrSql += " and CA_NAME like '%" + TextBox1.Text.Trim() + "%' ";
            }           
        }
        StrSql += " and  ( SPDM  IN (SELECT DISTINCT spdm FROM [ProductUpDown] WHERE [ISUP]=1) ) ";

        DataSet ds = new BLL.SHANGPIN().GetListMULU(
            "SHANGPIN LEFT JOIN COMMODITY_AUTO ON SHANGPIN.SPDM=COMMODITY_AUTO.CA_CODE  ",

            "CA_CODE, SHANGPIN.*,CA_NAME,CA_WEIGHT", "SPDM", false, pagesize, pageindex, "SPDM", true, StrSql, out pageCount, out rowCount);
        //绑定商品列表
        ShangpinList1.SPDM = spdm;
        ShangpinList1.Bind(ds.Tables[0]);
        AspNetPager1.RecordCount = rowCount;
    }
}
